package com.example.administrator.szb.fragments.luyan;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.HomeActivity;
import com.example.administrator.szb.activity.luyan.LuYanDeatilActivity;
import com.example.administrator.szb.activity.luyan.LuYanNewActivity;
import com.example.administrator.szb.activity.luyan.presenter.MyLuyanPresenter;
import com.example.administrator.szb.activity.luyan.view.MyLuyanView;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.fragments.fragment_forTab.home.adapter.LuYanadapter;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.LuYanList;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.QTLog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/1/22.
 */

public class FragmentMyLuYan extends MVPBaseFragment<MyLuyanView,MyLuyanPresenter> implements  MyLuyanView {

    private SmartRefreshLayout refreshLayout;
    private int types = 0;//0我发布的 1我报名的

    String url;
    private View error_net_ll;
    private TextView tv_net_error;

    private ImageView iv_error2;
    private Button error_btn;
    private ListView listView;
    private boolean isFirstEnter=true;
    private int current_page = 1;//列表当前页
    private int per_page = 10;//默认展示条数
    private LuYanadapter adapter;
    private List lists=new ArrayList();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_shoucang_jg,null);
    }

    @Override
    protected MyLuyanPresenter createPresenter() {
        mPresenter = new MyLuyanPresenter(this);
        return mPresenter;
    }
    public void setType(int type) {
        types = type;

    }

    @Override
    protected void initView(View view) {
        iv_error2 = view.findViewById(R.id.iv_error2);
        refreshLayout = view.findViewById(R.id.zx_fragment_swiperefreshlayout);
        listView = view.findViewById(R.id.listView);
        listView.setDivider(null);
        listView.setDividerHeight(0);
        error_net_ll = view.findViewById(R.id.error_net_ll);
        error_btn = view.findViewById(R.id.home_fragment_button_djcs_error);
        tv_net_error = view.findViewById(R.id.tv_net_error);
    }


    @Override
    protected void initEvent() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, LuYanDeatilActivity.class);
                if (types==0){
                    intent.putExtra("showPhone",true);
                }
                intent.putExtra("id",((LuYanList)lists.get(position)).getId());
                startActivity(intent);
            }
        });


        refreshLayout.setEnableLoadMore(true);//是否启用上拉加载功能
        refreshLayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                current_page++;
                requestReferList(1, current_page,per_page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                current_page=1;
                requestReferList(0, 1,per_page);
            }
        });

    }

    @Override
    protected void initData() {
        adapter = new LuYanadapter(context,lists);
        listView.setAdapter(adapter);
        if (isFirstEnter) {
            isFirstEnter = false;
            refreshLayout.autoRefresh();
        }
    }
    /**
     *
     * @param type 其中传入0代表刷新，1代表加载更多
     * @param page
     * @param count
     */
    private void requestReferList(final int type, int page, int count){
        error_net_ll.setVisibility(View.GONE);
        mPresenter.getAll(page,count,types);
    }

    private void initAdater(){
        if (adapter==null){
            adapter = new LuYanadapter(context,lists);
            listView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showList(List list) {
        int type=1;
        if (current_page==1){
            type=0;
        }
        if (type==1){
            //加载更多
            refreshLayout.finishLoadmore();

            if (list.size()<per_page){

                //没有更多了
                refreshLayout.setEnableLoadMore(false);

            }
        }else {
            lists.clear();

            //刷新
            refreshLayout.finishRefresh();
            refreshLayout.setEnableLoadMore(true);
            current_page=1;
        }
        lists.addAll(list);
        if (lists.size()==0){

            iv_error2.setImageResource(R.mipmap.no_data_icon);
            QTLog.e(types);
            if (types==0){
                tv_net_error.setText("您还没有发布过路演/调研");
                error_btn.setText("去发布");
                error_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CheckInfoUtils.check(activity, iosDiaolog, error_btn, new CheckInfoUtils.OnSuccessLietener() {
                            @Override
                            public void onSuccess() {

                                Intent intent  = new Intent(context, LuYanNewActivity.class);
                                startActivity(intent);
                            }
                        });
                    }
                });
            }else {
                tv_net_error.setText("您还没有报名过路演/调研");
                error_btn.setText("去报名");
                error_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CheckInfoUtils.check(activity, iosDiaolog, error_btn, new CheckInfoUtils.OnSuccessLietener() {
                            @Override
                            public void onSuccess() {
                                HomeActivity.canChangeTab=true;
                                HomeActivity.canChangeTabIndex=1;
                                Intent intent  = new Intent(context, HomeActivity.class);
                                startActivity(intent);
                                getActivity().finish();
                            }
                        });

                    }
                });

            }



            error_net_ll.setVisibility(View.VISIBLE);
        }
        initAdater();
    }

    @Override
    public void onSuccesss(String str) {

    }

    @Override
    public void onFails(String string) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        tv_net_error.setText("网络异常,无法连接网络");
        iv_error2.setImageResource(R.mipmap.error_icon);
        error_btn.setText("点击重试");
        error_btn.setVisibility(View.VISIBLE);
        error_net_ll.setVisibility(View.VISIBLE);
        error_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshLayout.autoRefresh();
                requestReferList(0, 1,per_page);
            }
        });
    }
}
